package com.ruoyi.server.encode;

import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.server.encrypt.AuthEncryption;
import com.ruoyi.server.pkg.SendMsg;
import com.ruoyi.server.utils.OnlineUtil;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageEncoder;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;

import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * 出
 *
 * @author OpenACloud
 * @create 2024/6/3 21:30
 */
public class ReceiveSendEncode extends MessageToMessageEncoder<SendMsg> {

    static boolean IS_OPEN = false;

    @Override
    protected void encode(ChannelHandlerContext ctx, SendMsg sendMsg, List<Object> objArrays) throws Exception {
        if(IS_OPEN) {
            int numAuth = RandomUtil.randomBoolean() ? RandomUtil.randomInt(100, 124) : RandomUtil.randomInt(-100, -124);
            ctx.attr(OnlineUtil.AUTH_BYTE).set(new Long(numAuth));
            sendMsg.CONTEXTAUTH = AuthEncryption.encryption(numAuth);
        }
        objArrays.add(new TextWebSocketFrame(JSONObject.toJSONString(sendMsg)));
    }
}
